using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace iwss.Document.Features.iwss_Document
{
    /// <summary>
    /// 此类用于处理在激活、停用、安装、卸载和升级功能的过程中引发的事件。
    /// </summary>
    /// <remarks>
    /// 附加到此类的 GUID 可能会在打包期间使用，不应进行修改。
    /// </remarks>

    [Guid("14fa8224-73f6-4ca6-a1c0-66118f47d316")]
    public class iwss_DocumentEventReceiver : SPFeatureReceiver
    {
        // 取消对以下方法的注释，以便处理激活某个功能后引发的事件。

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            { 
                SPWeb web = properties.Feature.Parent as SPWeb;
                web.AllowUnsafeUpdates = true;
                string permissionLevelName = "普通用户";
                // Create a new Permission Level
                SPRoleDefinition safeDef = safeDef = new SPRoleDefinition();
                safeDef.Name = permissionLevelName;
                safeDef.Description = "普通用户的查看编辑与上传权限";
                safeDef.BasePermissions =
                        SPBasePermissions.ViewFormPages |
                        SPBasePermissions.ViewListItems |
                        SPBasePermissions.OpenItems |
                        SPBasePermissions.BrowseDirectories |
                        SPBasePermissions.BrowseUserInfo |
                        SPBasePermissions.ViewPages |
                        SPBasePermissions.Open |
                        SPBasePermissions.ManagePersonalViews |
                        SPBasePermissions.AddDelPrivateWebParts |
                        SPBasePermissions.UpdatePersonalWebParts;

                SPRoleDefinitionCollection roleDefs = web.RoleDefinitions;
                bool hasPermissioned = false;
                foreach (SPRoleDefinition item in roleDefs)
                {
                    if (item.Name == permissionLevelName)
                    {
                        hasPermissioned = true;
                        break;
                    }
                }

                // Add the permission level to web
                if (!hasPermissioned)
                    web.RoleDefinitions.Add(safeDef);


                //// Bind to the permission level we just added
                //safeDef = web.RoleDefinitions[permissionLevelName];
                //// Create a new role Assignment using the SharePoint Group "Foo"
                //SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)web.SiteGroups["Foo"]);
                //// Add the Permission Level to the Foo SharePoint Group
                //roleAssignment.RoleDefinitionBindings.Add(safeDef);
                //// Add the new Role Assignment to the web
                //web.RoleAssignments.Add(roleAssignment);

                SPList ShareLib = web.Lists.TryGetList("共享库");
                SPList DocLib = web.Lists.TryGetList("文档库");

                DocLib.BreakRoleInheritance(false, true);
                ShareLib.BreakRoleInheritance(false, true);

                SPUser user = web.EnsureUser("c:0!.s|windows");
                SPRoleAssignment allWindowsUserAssign = new SPRoleAssignment(user.LoginName, user.Email, user.Name, user.Notes);
                allWindowsUserAssign.RoleDefinitionBindings.Add(web.RoleDefinitions["设计"]);
                allWindowsUserAssign.RoleDefinitionBindings.Add(web.RoleDefinitions["参与讨论"]);
                allWindowsUserAssign.RoleDefinitionBindings.Add(web.RoleDefinitions["读取"]);
                allWindowsUserAssign.RoleDefinitionBindings.Add(web.RoleDefinitions["普通用户"]);

                DocLib.RoleAssignments.Add(allWindowsUserAssign);
                ShareLib.RoleAssignments.Add(allWindowsUserAssign);
                
                //添加所有用户至当前网站，给予读取访问权限
                SPRoleAssignment allWindowsUserAss = new SPRoleAssignment(user.LoginName, user.Email, user.Name, user.Notes);
                allWindowsUserAss.RoleDefinitionBindings.Add(web.RoleDefinitions["受限访问"]);
                allWindowsUserAss.RoleDefinitionBindings.Add(web.RoleDefinitions["读取"]);
                web.RoleAssignments.Add(allWindowsUserAss);

                web.AllowUnsafeUpdates = false;
            }
            catch (Exception)
            {

            }
        }


        // 取消对以下方法的注释，以便处理在停用某个功能前引发的事件。

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // 取消对以下方法的注释，以便处理在安装某个功能后引发的事件。

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // 取消对以下方法的注释，以便处理在卸载某个功能前引发的事件。

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // 取消对以下方法的注释，以便处理在升级某个功能时引发的事件。

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
